package com.contactbook.web.service;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import contactbook.service.AccountServiceInterface;
import contactbook.service.ContactServiceInterface;
import contactbook.service.GroupServiceInterface;

public class ServiceLocator {
	
	private static final ServiceLocator instance = new ServiceLocator();
	private InitialContext ctx;
	
	private ServiceLocator() {
		try {
			ctx = new InitialContext();
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public static ServiceLocator getInstance() {
		return instance;
	}

	public ContactServiceInterface getContactService() {
		try {
			ContactServiceInterface service = (ContactServiceInterface) ctx.lookup(ContactServiceInterface.JNDI);
			return service;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public GroupServiceInterface getGroupService() {
		try {
			GroupServiceInterface service = (GroupServiceInterface) ctx.lookup(GroupServiceInterface.JNDI);
			return service;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public AccountServiceInterface getAccountService() {
		try {
			AccountServiceInterface service = (AccountServiceInterface) ctx.lookup(AccountServiceInterface.JNDI);
			return service;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
